<?php

namespace App\Kuafu\Config\Controller\Merchant;

use App\Kuafu\Config\Enum\ConfigCodeEnum;
use App\Kuafu\Config\Enum\ConfigEnum;
use App\Kuafu\Config\Service\ConfigService;
use App\Kuafu\Config\Validate\ConfigValidate;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\ShopService;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;
use Illuminate\Support\Collection;

class ConfigController extends BaseController
{

    /**
     * 获取版本强制更新信息
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Kuafu\Config\Exceptions\ConfigException
     */
    public function getAppVersionInfo(Request $request)
    {
        if (!$request->has('appNameType')) {
            $request->merge(['appNameType' => ConfigEnum::APP_NAME_TYPE_BUSINESS]);
        }
        //校验输入
        if ($valid = ConfigValidate::versionUpdateInputCheck($request)) {
            return $this->fail(ConfigCodeEnum::ERR_CONFIG_PARAM, $valid);
        }
        $shopId = (int)$request->header('shopId', 0);// 门店id

        $type = ConfigEnum::APP_TYPE_MAPPING_TABLE_TYPE[$request->appType];

        $type = (ConfigEnum::APP_NAME_TYPE_KEY_MAP[$request->appNameType] ?? '') . $type;

        $lists = ConfigService::listByTypeAndKey($type);

        $collection = new Collection(array_map(function ($v) {
            return json_decode($v, true);
        }, array_values($lists)));

        $maxVersionInfo = $collection->where('appNameType', $request->appNameType)
                                     ->where('nowVersion', $collection->max('nowVersion'))
                                     ->where('nowVersion', '>', $request->nowVersion)
                                     ->first();

        // 夸父收银-ipad 根据门店id/省份/城市匹配版本更新信息
        if ($maxVersionInfo && $shopId && $request->appNameType == ConfigEnum::APP_NAME_TYPE_POS_IPAD) {
            $shopDetail = ShopService::getShopInfo($shopId);
            if (!$shopDetail) {
                return $this->success(['versionInfo' => new \stdClass()]);
            }
            if ((isset($maxVersionInfo['shopId']) && $maxVersionInfo['shopId']) ||
                (isset($maxVersionInfo['provinceCode']) && $maxVersionInfo['provinceCode']) ||
                (isset($maxVersionInfo['cityCode']) && $maxVersionInfo['cityCode'])) {
                $res1 = $res2 = $res3 = false;
                if (isset($maxVersionInfo['shopId']) && $maxVersionInfo['shopId'] &&
                    in_array($shopId, $maxVersionInfo['shopId'])) {// 按照门店id
                    $res1 = true;
                }
                if (isset($maxVersionInfo['provinceCode']) && $maxVersionInfo['provinceCode'] &&
                    in_array($shopDetail['province'], $maxVersionInfo['provinceCode'])) {// 按照省份code
                    $res2 = true;
                }
                if (isset($maxVersionInfo['cityCode']) && $maxVersionInfo['cityCode'] &&
                    in_array($shopDetail['city'], $maxVersionInfo['cityCode'])) {// 按照城市code
                    $res3 = true;
                }
                if (!$res1 && !$res2 && !$res3) {
                    return $this->success(['versionInfo' => new \stdClass()]);
                }
            }
        }

        return $this->success(['versionInfo' => $maxVersionInfo ?? new \stdClass()]);
    }
}
